<?php

namespace App\Admin\Controllers;

use App\crm_supplier;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_supplierController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('供应商管理');
            $content->description('供应商列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('供应商管理');
            $content->description('供应商列表');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('供应商管理');
            $content->description('供应商添加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    // 供应商表
    protected function grid()
    {
        return Admin::grid(crm_supplier::class, function (Grid $grid) {

            $grid->id('序号')->sortable();
            $grid->supplier_logoname('品牌名称');
            // $grid->supplier_type('支付方式')->editable('textarea');
            $grid->supplier_name('公司名称');
            $grid->Admin_users()->username('添加人');
            $grid->filter(function ($filter) {
                $filter->useModal();
                $filter->like('supplier_logoname','品牌名称');
                $filter->like('supplier_name','公司名称');
                $filter->like('Admin_users.username','添加人');
            });
            $grid->actions(function ($actions) {
                // append一个查看详情操作
                $actions->append("<a href='".admin_url('supplier_type/'.$actions->getKey())."'><i class='fa fa-eye'></i></a>");
            });
            // $grid->created_at();
            // $grid->updated_at();
        });
    }
    // 账户类别表
    protected function grid2()
    {
        return Admin::grid(crm_supplier::class, function (Grid $grid) {

            $grid->id('序号')->sortable();
            $grid->supplier_logoname('品牌名称');
            // $grid->supplier_type('支付方式')->editable('textarea');
            $grid->supplier_name('公司名称');
            $grid->Admin_users()->username('添加人');
            $grid->filter(function ($filter) {
                $filter->useModal();
                $filter->like('supplier_logoname','品牌名称');
                $filter->like('supplier_name','公司名称');
                $filter->like('Admin_users.username','添加人');
            });
            
            // $grid->created_at();
            // $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_supplier::class, function (Form $form) {

            $form->display('id', '序号');
            $form->text('supplier_logoname','品牌名称')->rules('required');
            $form->text('supplier_name','公司名称')->rules('required');
            // $form->text('supplier_type.typename','账户类别')->rules('required');
            // $form->text('supplier_type.zhanghao','账号')->rules('required');
            $form->hidden('staff_id','员工id')->default(Admin::user()->id);
            $form->tools(function (Form\Tools $tools) {
                // 去掉跳转列表按钮
                $tools->disableListButton();
            });
            // $form->display('created_at', 'Created At');
            // $form->display('updated_at', 'Updated At');
        });
    }
}
